package com.volcreate.inn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.volcreate.inn.mapper.TypeMapper;
import com.volcreate.inn.po.Type;
import com.volcreate.inn.service.ITypeService;
import com.volcreate.inn.vo.TypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zxc
 * @since 2020-08-23
 */
@Service
public class TypeService extends ServiceImpl<TypeMapper, Type> implements ITypeService {

    @Autowired
    private TypeMapper typeMapper;


    @Override
    public Type getByName(String name) {
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        wrapper
                .eq("name", name);
        return typeMapper.selectOne(wrapper);
    }


    @Override
    public PageInfo<Type> list(int page, int size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(typeMapper.selectList(null));
    }

    @Override
    public List<TypeVO> listByBlogSizeDesc() {
        return typeMapper.selectByBlogSizeDesc();
    }

    /**
     * 多表查询时，分页查询不适用，只能进行一次分页查一次
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<TypeVO> listByBlogSizeDesc(int page, int size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(typeMapper.selectByBlogSizeDesc());
    }

    /**
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Type> listByGmtModifiedDesc(int page, int size) {
        PageHelper.startPage(page, size);
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_modified");
        return new PageInfo<>(typeMapper.selectList(wrapper));
    }


}
